/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.front;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.wechat.entity.Product;
import com.boxin.ims.modules.wechat.entity.ReturnResult;
import com.boxin.ims.modules.wechat.entity.Seckill;
import com.boxin.ims.modules.wechat.entity.SeckillConfig;
import com.boxin.ims.modules.wechat.entity.SeckillGainer;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.ProductService;
import com.boxin.ims.modules.wechat.service.SeckillGainerService;
import com.boxin.ims.modules.wechat.service.SeckillService;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.boxin.ims.modules.wechat.utils.SeckillUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 产品Controller
 * @author Jakemanse
 * @version 2013-06-28
 */
@Controller
@RequestMapping(value = Global.FRONT_PATH+"/wechat/product")
public class ProductFrontController extends BaseController {

	@Autowired
	private ProductService productService;
	
	@Autowired
	private SeckillService seckillService;
	
	@Autowired
	private  SeckillGainerService seckillGainerService;
	
	@Autowired
	private WechatMemberService wechatMemberService;
	
	
	
	@ModelAttribute
	public Product get(@RequestParam(required=false) Long id) {
		if (id != null){
			return productService.get(id);
		}else{
			return new Product();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Product product, HttpServletRequest request, HttpServletResponse response, Model model) {
//		User user = UserUtils.getUser();
//		if (!user.isAdmin()){
//			product.setUser(user);
//		}
		String category  = request.getParameter("category");
		String subCategory = request.getParameter("subCategory");
		if(StringUtils.isNotEmpty(category)  && !"0".equals(category)) {
			product.setCategory(Integer.parseInt(category));
		}
		
		if(StringUtils.isNotEmpty(subCategory) && !"0".equals(subCategory)){
			product.setSubCategory(Integer.parseInt(subCategory));
		}
		
		
		
        Page<Product> page = productService.find(new Page<Product>(request, response), product); 
        model.addAttribute("page", page);
        model.addAttribute("category", category);
        model.addAttribute("subCategory", subCategory);
        model.addAttribute("pagination", page.simplePagination());
		return "modules/wechat/front/productList";
	}
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-27  下午3:31:04
	 * @function <p> 秒杀提交处理 </p>
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "doseckill")
	public String doseckill( HttpServletRequest request, HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		
		Seckill seckill = new Seckill();
		Long userId = 0L;
		Long seckillId = 0L;
		try {
			seckillId = Long.parseLong(request.getParameter("seckill.id"));
			userId = 	Long.parseLong(request.getParameter("uid"));
		} catch (Exception e) {
			model.addAttribute("message","您访问的页面不存在!");
			return "modules/wechat/front/seckillsError";
		}
		
		seckill.setId(seckillId);
		User user = new User();
		user.setId(userId);
		String wechatId  = request.getParameter("wechatId");
		request.setAttribute("wechatId", request.getParameter("wechatId"));
		
		 WechatMember member = (WechatMember)	request.getSession(true).getAttribute("wechatUser"+userId);
		
		ReturnResult rrResult = SeckillUtils.doSeckill(seckill, member);
		SeckillConfig config = SeckillUtils.getSeckillConfigByUserId(userId);
		model.addAttribute("seckill",seckill);
		model.addAttribute("config",config);
		model.addAttribute("member",member);
		model.addAttribute("returnResult", rrResult);
		if(rrResult.isSuccess()){
			SeckillGainer sg = (SeckillGainer) rrResult.getParam();
				seckill = seckillService.get(seckill.getId());
				model.addAttribute("seckillGainer",sg);
				return "modules/wechat/front/seckillsOk";
		}else{
			model.addAttribute("message",rrResult.getInfo());
			return "modules/wechat/front/seckillsError";
		}
	}
	
	
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-16  上午11:58:49
	 * @function <p> 进入秒杀页面 </p>
	 * @param userId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "seckills")
	public String seckillist( HttpServletRequest request, HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		
		
		String id = request.getParameter("uid");				//userId
		String wechatId = request.getParameter("wechatId");		//OpenId
		String mobile   = request.getParameter("mobile");		//Mobile
		Long userId = 0L;
		try {
			userId = Long.parseLong(id);
		} catch (Exception e) {
			userId = 0L;
		}
		
		User user = new User();
		user.setId(userId);
		
		//需要判断是否已经登录过
		WechatMember member = (WechatMember)	request.getSession(true).getAttribute("wechatUser"+userId);
		if(member == null  ){	//没有登录先进入登录页面
			if( "34".equals(id)){	//阳光麦康处理
				member = wechatMemberService.getByWechatMemberId(user, wechatId);
				request.getSession(true).setAttribute("wechatUser"+userId, member);
			}else{
				request.setAttribute("topage","seckill");
				request.setAttribute("wechatId",wechatId);
				request.setAttribute("id",id);
				request.setAttribute("uid",id);
				//return "redirect:" + Global.FRONT_PATH+ "/wechat/login/"+id;
				return "modules/wechat/front/wechatLogin";
				
			}
		}		
		
		
		
		Seckill seckill = new Seckill();
		
		seckill.setUser(user);
		Page<Seckill> page = seckillService.find(new Page<Seckill>(request, response), seckill);
		model.addAttribute("page", page);
		model.addAttribute("nowDate",DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
		model.addAttribute("wechatId",wechatId);
		model.addAttribute("uid",id);
		
		
		SeckillConfig config = SeckillUtils.getSeckillConfigByUserId(userId);
		
		model.addAttribute("config",config);
		model.addAttribute("member",member);
		
		//判断是否微信浏览器
		if(request.getHeader("user-agent").contains("MicroMessenger")){	// 是微信
			//判断是否是盗链  (openId + ip =唯一)
			
		}else{
			addMessage(redirectAttributes, "请在微信中打开此页面!");
		}
		
		
		return "modules/wechat/front/seckills";
	}
	
	
	@RequestMapping(value = {"detail/{id}"+Global.URL_SUFFIX})
	public String detail(@PathVariable("id") Long id,Model model, HttpServletRequest request, HttpServletResponse response ) {
//		User user = UserUtils.getUser();
//		if (!user.isAdmin()){
//			product.setUser(user);
//		}
		Product product = productService.get(id);
		model.addAttribute("product", product);
		
		return "modules/wechat/front/productDetail";
	}
	
	
	



}
